from moodlefuse.settings import MOODLE_WEBSITE_URL

class SubmitAssignmentFile(object):

    def __init__(self, browser):
        self.browser = browser

    def submit(self, file):
        self.browser.open(file.assignment_url)
        
        form_action = '%s/mod/assignment/upload.php' % MOODLE_WEBSITE_URL
        self.browser.select_form(predicate=lambda form: form.action == form_action)

        self.browser.form.find_control("newfile").readonly = False
        self.browser.form.add_file(open(file.filepath,"rb"), "", file.name, name='newfile')

        resp = self.browser.submit()
  